package com.hzw.code.common.utils;

import org.apache.commons.lang3.StringUtils;
import java.io.Serializable;


/**
 * 通用返回结果类
 *
 * @author: 胡汉三
 * @date: 2020/5/18 10:00
 */
public class ActionResult<T> implements Serializable {

    /**
     * 序列号
     */
    private static final long serialVersionUID = 3057249770603287296L;

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 成功标识
     */
    private boolean success;

    /**
     * 消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 构造方法
     */
    public ActionResult() {

    }

    /**
     * 构造方法
     * @param resultCode
     */
    public ActionResult(ResultCodeEnum resultCode) {
        this.setSuccess(ResultCodeEnum.SUCCESS.getValue().equals(resultCode.getValue()));
        this.setCode(resultCode.getValue());
        this.setMessage(resultCode.getDesc());
        this.setData(null);
    }

    /**
     * 构造方法
     * @param resultCode
     * @param msg
     */
    public ActionResult(ResultCodeEnum resultCode, String msg) {
        this.setSuccess(ResultCodeEnum.SUCCESS.getValue().equals(resultCode.getValue()));
        this.setCode(resultCode.getValue());
        this.setMessage(StringUtils.isNotBlank(msg) ? msg : resultCode.getDesc());
        this.setData(null);
    }

    /**
     * 构造方法
     * @param resultCode
     * @param msg
     */
    public ActionResult(Integer resultCode, String msg) {
        this.setSuccess(ResultCodeEnum.SUCCESS.getValue().equals(resultCode));
        this.setCode(resultCode);
        this.setMessage(msg);
        this.setData(null);
    }

    /**
     * 构造方法
     * @param resultCode
     * @param message
     * @param data
     */
    public ActionResult(ResultCodeEnum resultCode, String message, T data) {
        this.setSuccess(ResultCodeEnum.SUCCESS.getValue().equals(resultCode.getValue()));
        this.setCode(resultCode.getValue());
        this.setMessage(StringUtils.isNotBlank(message) ? message : resultCode.getDesc());
        this.setData(data);
    }

    /**
     * 构造方法
     * @param resultCode
     * @param message
     * @param data
     */
    public ActionResult(Integer resultCode, String message, T data) {
        this.setSuccess(ResultCodeEnum.SUCCESS.getValue().equals(resultCode));
        this.setCode(resultCode);
        this.setMessage(message);
        this.setData(data);
    }
}
